package com.toKnow.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public final class SessionAndCookieUtil {
    /**
     * 设置cookie，通过token
     */
    public static void setUserCookie(HttpServletRequest request, HttpServletResponse response,
                                    String username, String userpwd, int expiry){
        String userCookieToken = JwtUserCookieUtil.sign(username, userpwd);
        Cookie cookie = new Cookie("userToken", userCookieToken);
        cookie.setMaxAge(expiry);
        cookie.setPath(request.getContextPath());
        response.addCookie(cookie);
    }

    /**
     * 设置session，通过token
     */
    public static void setUserSession(HttpServletRequest request,
                                      String id, String username, String userpwd){
        String userSessionToken = JwtUserSessionUtil.sign(id, username, userpwd);
        HttpSession session = request.getSession();
        session.setAttribute("userToken", userSessionToken);
    }

}
